package controller.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Film.FilmInfo;
import model.util.FilmHeader;
import service.FilmService;
import service.util.ServiceException;
import service.util.ServiceFactory;
import controller.util.MessageHolder;
import dao.util.DaoContextEnum;

/**
 * Servlet implementation class FilmeServlet
 */
@SuppressWarnings("serial")
public class FilmeServlet extends HttpServlet {
	
	private ArrayList<FilmInfo> info;
	MessageHolder holder = new MessageHolder(); 
	FilmHeader titel = new FilmHeader();
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			FilmService service = (FilmService) ServiceFactory.getServiceForName("FilmService", DaoContextEnum.MYSQL);  
			info = service.getFilmInfos(request.getParameter("datentraeger"));
			request.setAttribute("titel", titel);
			request.setAttribute("info", info);	
			request.getRequestDispatcher("filme.jsp").forward(request, response);
			return;
			
		} catch (ServiceException e){

			holder.setMessage(e.getMessage());
			request.setAttribute("message", holder);
			request.getRequestDispatcher("filme.jsp").forward(request, response);
			return;
		}
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
